package com.atguigu.schedule.controller;

import com.atguigu.schedule.common.Result;
import com.atguigu.schedule.pojo.SysSchedule;
import com.atguigu.schedule.service.SysScheduleService;
import com.atguigu.schedule.service.impl.SyscheduleServiceimpl;
import com.atguigu.schedule.util.WebUtil;
import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet(value = "/sysSchedule/*", name = "SysScheduleController")
public class SysScheduleController extends BashController {
    private SysScheduleService scheduleService = new SyscheduleServiceimpl();

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("add 方法");
    }

    protected void findAllSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid = Integer.parseInt(req.getParameter("uid"));
        List<SysSchedule> itemList = scheduleService.findItemListByUid(uid);
        Map data=new HashMap<>();
        data.put("itemList", itemList);
        //将用户的所有日程放入Result对象
        Result<Map> mapResult = Result.ok(data);
        //返回客户端
        WebUtil.writeJson(resp, mapResult);

    }
    protected void addDefaultSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid = Integer.parseInt(req.getParameter("uid"));
        scheduleService.addDefaultSchedule(uid);
        WebUtil.writeJson(resp, Result.ok(null));

    }


    protected void updateSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取客户端发送过来的数据
        SysSchedule schedule = WebUtil.readJson(req, SysSchedule.class);
        scheduleService.updateSchedule(schedule);
        WebUtil.writeJson(resp, Result.ok(null));

    }
    protected void deleteSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取客户端发送过来的数据
        int sid = Integer.parseInt(req.getParameter("sid"));
        scheduleService.deleteSchedule(sid);
        WebUtil.writeJson(resp, Result.ok(null));

    }

}

